<template>
  <van-icon
    :color="value ? 'red' : '#777'"
    :name="value ? 'star' : 'star-o'"
    @click="clickCollected"
  />
</template>

<script>
import { addCollected, delCollected } from '@/api/articles.js'
export default {
  data() {
    return {

    }
  },

  props: {
    value: {
      type: Boolean,
      required: true
    },
    artId: {
      type: [Object, Number],
      required: true
    }
  },

  created() {

  },

  methods: {
    async clickCollected() {
      this.$toast.loading({
        message: '加载中...',
        duration: 0, // 持续展示 toast
        forbidClick: true
      })
      try {
        if (this.value) {
          // 收藏 进行 取消收藏
          await delCollected(this.artId)
        } else {
          // 未收藏 进行 收藏
          await addCollected(this.artId + '')
        }
        this.$toast('操作成功')
        this.$emit('input', !this.value)
      } catch (err) {
        this.$toast('收藏失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
</style>
